var arr = new Array; // Array of free slots
var items = new Array;
arr.length = 9;
var canAct = false;
var isII = false;

function step(x,y) {
         if (!canAct) return
         var component = Qt.createComponent("Xor.qml");
         if (component.status == Component.Ready) {
             // Find clicked square indexes
             var i = x/100
             var j = y/90
             var newX = 0
             var newY = 0

             if (i>0 && i<1) { newX = 0; i = 0 }
             else if (i>1 && i<2) { newX = 100; i = 1; }
             else if (i>2 && i<3) { newX = 200; i = 2; }

             if (j>2 && j<3) { newY = 180; j = 2 }
             else if (j>0 && j<1) { newY = 0; j = 0; }
             else if (j>1 && j<2) { newY = 90; j = 1; }

             var ind = i*3 + j
             if (arr[ind] != 0)
                 return;
             var newItem = component.createObject(container);
             if (tick)
                 newItem.source = "o.png"
             newItem.index = ind
             arr[ind] = tick? -1 : 1;
             tick = !tick
             items.push(newItem)

             newItem.x = newX + 90
             newItem.y = newY + 5
             newItem.opacity = 1

             checkState()

             if (isII)
                 computerStep()
         }
     }

function computerStep()
{
    if (!canAct) return
    var step = -1;
    while(true)
    {
        step = Math.round(Math.random()*10)
        if (step>8) step = 8
        if (arr[step] == 0)
            break;
    }

    var compJ = step%3
    var compI = (step - compJ) / 3

    var comp = Qt.createComponent("Xor.qml")
    if (comp.status == Component.Ready)
    {
        var compItem = comp.createObject(container);

        var newX = 0
        var newY = 0

        if (compI == 0) { newX = 0 }
        else if (compI == 1) { newX = 100 }
        else if (compI == 2) { newX = 200 }

        if (compJ == 2) { newY = 180 }
        else if (compJ == 0) { newY = 0 }
        else if (compJ == 1) { newY = 90 }


        if (tick)
            compItem.source = "o.png"
        arr[step] = tick? -1 : 1;
        tick = !tick
        compItem.index = step
        items.push(compItem);

        compItem.y = newY + 5
        compItem.x = newX + 90
        compItem.opacity = 1

        checkState()
    }

}

function newGame(onII)
{
    isII = onII;
    console.log("In new game!")
    tick = false
    canAct = true
    for (var i=0; i<9; i++)
    {
        arr[i] = 0
    }
    for (var i=0; i<items.length; i++)
        items[i].destroy()
    items.length = 0
    console.log(colorButton)
    colorButton = false
}

function checkState()
{
    if (compare(arr[0], arr[1], arr[2])) { hightlightWinner(0,1,2,arr[1]); canAct = false; return }
    else if (compare(arr[3], arr[4], arr[5])) { hightlightWinner(3,4,5,arr[4]); canAct = false; return }
    else if (compare(arr[6], arr[7], arr[8])) { hightlightWinner(6,7,8,arr[7]); canAct = false; return }
    else if (compare(arr[0], arr[3], arr[6])) { hightlightWinner(0,3,6,arr[3]); canAct = false; return }
    else if (compare(arr[1], arr[4], arr[7])) { hightlightWinner(1,4,7,arr[4]); canAct = false; return }
    else if (compare(arr[2], arr[5], arr[8])) { hightlightWinner(2,5,8,arr[5]); canAct = false; return }
    else if (compare(arr[0], arr[4], arr[8])) { hightlightWinner(0,4,8,arr[4]); canAct = false; return }
    else if (compare(arr[6], arr[4], arr[2])) { hightlightWinner(6,4,2,arr[4]); canAct = false; return }

    var anyStepsAvailable = false;
    for (var i=0; i<arr.length; i++)
        if (arr[i] == 0)
            anyStepsAvailable = true;

    if (!anyStepsAvailable)
    {
        canAct = false
        colorButton = true
    }
}

function hightlightWinner(a, b, c, tm)
{
    for(var i=0; i<items.length;i++)
    {
        var v = items[i].index
        if (v == a || v == b || v == c)
        {
            if (tm == 1)
                items[i].source = "xr.png"
                else items[i].source = "or.png"
        }
    }
    colorButton = true
}

function compare(a,b,c)
{
    if (a == 0 || b == 0 || c == 0) return false
    if ((a==b)&&(b==c)&&(c==a)) return true
}
